package com.azy.service.impl;

import com.azy.mapper.ProductsDao;
import com.azy.pojo.entity.Products;
import com.azy.pojo.vo.ProductsVo;
import com.azy.service.ProductsService;
import com.azy.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ fileName:ProductsServiceImpl
 * @ description:
 * @ author:Azy
 * @ createTime:2023/4/12 19:10
 * @ version:1.0.0
 */
@Service
public class ProductsServiceImpl implements ProductsService {
    @Resource
    private ProductsDao productsDao;
    @Override
    public Result findByPage(Integer current, Integer pageSize, ProductsVo productsVo) {
        Page<Products> page = new Page<>(current, pageSize);
        QueryWrapper<Products> wrapper = new QueryWrapper<>();
       if(StringUtils.hasText(productsVo.getPid())){
            wrapper.like("pid",productsVo.getPid());
        }
        if (StringUtils.hasText(productsVo.getPname())){
            wrapper.like("pname",productsVo.getPname());
        }
        IPage<Products> productsIPage = productsDao.selectByPage(page, wrapper);
        return new Result(200,"分页条件查询商品成功",page);
    }

    @Override
    public Result findById(String pid) {
        return new Result(200,"查询单个商品成功",productsDao.selectById(pid));
    }

    @Override
    public Result deleteById(String pid) {
        return new Result(200,"根据id删除成功",productsDao.deleteById(pid));
    }

    @Override
    public Result insert(Products products) {
        return new Result(200,"新增单条数据成功",productsDao.insert(products));
    }


    @Override
    public Result update(Products products) {
        return new Result(200,"根据id修改成功",productsDao.updateById(products));
    }



}
